﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Automation.Protocols.Dao;
using Automation.Protocols.Model;
using Common.Logging;
using SupportLibrary;
using SupportLibrary.Message;

namespace Automation.Dal.Excel.Dao
{
    public class TestStepDao : TestStepBase
    {
         private ExcelProvider _exProvider;
        private string _Path;
        private ILog log;
        public TestStepDao(string path)
        {
            log = LogManager.GetLogger(this.GetType());
            _Path = path;
            _exProvider = ExcelProvider.Create(@path);
            if (_exProvider == null)
                //MessageShow.FileDoesNotExist(path);
                throw new Exception(path + "doesnot exist!");
        }

        
         

        public override List<TestStep> getTeststeps()
        {
            List<TestStep> list = new List<TestStep>();
            //Console.WriteLine("READING SHEET TStep.");
            log.Debug(Resources.Information.msg_ReadingSheetTStep);
            try
            {
                if (_exProvider != null)
                {
                    var TestSteps = _exProvider.GetSheet<Automation.Dal.Excel.Model.TestStep>();
                    int i = 0;
                    if (TestSteps != null)
                    {
                        foreach (Automation.Dal.Excel.Model.TestStep item in TestSteps)
                        {
                            if (item.ID != null)
                            {
                                i++;
                                if (item.isValid())
                                    list.Add(new TestStep() { 
                                        ID = item.ID,
                                        Comment = item.Comment,
                                        Keyword = item.Keyword,
                                        Output = item.Output,
                                        Parameters = item.Parameters,
                                        TestCase = item.TestCase                                        
                                    });
                                else
                                {
                                    //MessageShow.InvalidRecord(i + 1);
                                    throw new Exception("Record "+(i+1)+" is invalid!");
                                }
                            }
                        }
                    }
                    //Console.WriteLine(i + " records were read in sheet TStep.");
                    log.Debug(Message.ReplaceMessage(Resources.Information.msg_NumofRecord, ByKey.ByNumber(), i.ToString()));
                }
            }
            catch (Exception e)
            {
                //MessageShow.ExceptionOccur("Read TStep sheet exception.", e);
                log.Error(Resources.ErrorMessage.ReadTStepSheetErr + e.GetType().ToString() + e.Message);
                throw new Exception(Resources.ErrorMessage.ReadTStepSheetErr + e.GetType().ToString());
            }
            return list;
        }

        public override void UpdateSteps(List<TestStep> Steps)
        {
            try
            {
                if (_exProvider != null)
                {
                    foreach (var i in Steps)
                    {
                        try
                        {
                            Automation.Dal.Excel.Model.TestStep item = new Automation.Dal.Excel.Model.TestStep()
                            {
                                ID = i.ID,
                                Comment = i.Comment,
                                Keyword = i.Keyword,
                                Output = i.Output,
                                Parameters = i.Parameters,
                                TestCase = i.TestCase
                            };
                            _exProvider.GetSheet<Automation.Dal.Excel.Model.TestStep>().UpdateOnSubmit(item);


                        }
                        catch (Exception ex)
                        {
                            log.Error(Resources.ErrorMessage.UpdateTStepErr + ex.GetType().ToString() + ex.Message);
                            throw new Exception(Resources.ErrorMessage.UpdateTStepErr + ex.GetType().ToString());
                        }
                        finally {
                            _exProvider.SubmitChanges();
                        }
                    }

                }
            }
            catch (Exception e)
            {
                //MessageShow.ExceptionOccur("Update TStep sheet exception.", e);
                log.Error(Resources.ErrorMessage.UpdateTStepErr + e.GetType().ToString() + e.Message);
                throw new Exception(Resources.ErrorMessage.UpdateTStepErr + e.GetType().ToString());
            }
        }
    }
}
